<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
 
<body>
  <form method="post" action="">
		你爱好的运动是？<input type="checkbox" id="checkedAllBox" />全选/全不选 
		<br />
		<input type="checkbox" name="items" value="足球" />足球
		<input type="checkbox" name="items" value="篮球" />篮球
		<input type="checkbox" name="items" value="羽毛球" />羽毛球
		<input type="checkbox" name="items" value="乒乓球" />乒乓球
		<br />
		<input type="button" id="checkedAllBtn" value="全　选" />
		<input type="button" id="checkedNoBtn" value="全不选" />
		<input type="button" id="checkedRevBtn" value="反　选" />
		<input type="button" id="sendBtn" value="提　交" />
	</form>
</body>
<script>
  var allBox = document.getElementById('checkedAllBox');
  var items = document.getElementsByName("items");
  // checked
  // 全选/全不选 
  allBox.onclick = function() {
    for (var i = 0; i < items.length; i++) {
      items[i].checked = this.checked
    }
  }
  // 全选
  var checkedAllBtn = document.getElementById('checkedAllBtn')
  checkedAllBtn.onclick = function () {
    for (var i = 0; i < items.length; i++) {
      items[i].checked = true
    }
    allBox.checked = true
  }

  // 全不选
  var checkedNoBtn = document.getElementById('checkedNoBtn')
  checkedNoBtn.onclick = function() {
    for (var i = 0; i < items.length; i++) {
      items[i].checked = false
    }
    allBox.checked = false
  }

  // 反选
  var checkedRevBtn = document.getElementById('checkedRevBtn')
  checkedRevBtn.onclick = function() {
    allBox.checked = true;
    for (var i = 0; i < items.length; i++) {
      items[i].checked = !items[i].checked
      if (!items[i].checked) {
        allBox.checked = false
      }
    }
  }

  // 提交
  var sendBtn = document.getElementById('sendBtn')
  sendBtn.onclick = function() {
    for (var i = 0; i < items.length; i++) {
      // 判断多选框是否选中
      if (items[i].checked) {
        alert(items[i].value)
      }
    }
  }

  for (var i = 0; i < items.length; i++) {
    //为四个多选框分别绑定点击响应函数
    items[i].onclick = function() {
      //将allBox设置为选中状态
      allBox.checked = true;
      for (var j = 0; j < items.length; j++) {
        //判断四个多选框是否全选
				//只要有一个没选中则就不是全选
        if (!items[j].checked) {
          //一旦进入判断，则证明不是全选状态
          //将allBox设置为没选中状态
          allBox.checked = false;
          //一旦进入判断，则已经得出结果，不用再继续执行循环
          break;
        }
      }
    }
  }
</script>
 
</html>